﻿using Admin.RazorLib;
using Client.BlazorMauiApp.Authentication;
using Client.BlazorWasmApp.Caches;
using DataServer.RazorLib;
using Microsoft.AspNetCore.Components.Authorization;
using MudBlazor.Services;
using Swimj.RazorLib;
using Swimj.RazorLib.Caches;
using System.IdentityModel.Tokens.Jwt;
using BaseData.RazorLib;
using Production.RazorLib;

namespace Client.BlazorMauiApp
{
    public static class MauiProgram
    {
        public static MauiApp CreateMauiApp()
        {
            var builder = MauiApp.CreateBuilder();
            builder
                .UseMauiApp<App>()
                .ConfigureFonts(fonts =>
                {
                    fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
                });

            builder.Services.AddMauiBlazorWebView();
#if DEBUG
            builder.Services.AddBlazorWebViewDeveloperTools();
#endif

            // 添加缓存            
            builder.Services.AddScoped<ISecureCache, LocalSecureCache>();

            builder.Services.AddOptions();
            // builder.Services.AddAuthorizationCore();

            builder.Services.AddScoped<JwtSecurityTokenHandler>();
            builder.Services.AddScoped<CustomAuthStateProvider>();
            builder.Services.AddScoped<AuthenticationStateProvider, CustomAuthStateProvider>();
            builder.Services.AddTransient<AuthHeaderHandler>();

            builder.Services.AddMudServices()
                .AddMudPageProcess()
                .AddMudBlazorDrawerService();

            builder.Services
                .AddHttpClient("adminApi", h =>
                {
                    h.BaseAddress = new Uri("http://localhost:5100");
                    // h.BaseAddress = new Uri("https://admin.webapi");
                }).AddHttpMessageHandler<AuthHeaderHandler>();

            builder.Services
                .AddHttpClient("dataserverApi", c =>
                {
                    c.BaseAddress = new Uri("http://localhost:5200");
                    // c.BaseAddress = new Uri("http://dataserver.webapi");
                })
                .AddHttpMessageHandler<AuthHeaderHandler>();

            builder.Services
                .AddHttpClient("productionApi", c =>
                {
                    c.BaseAddress = new Uri("http://localhost:5300");
                    // c.BaseAddress = new Uri("http://dataserver.webapi");
                })
                .AddHttpMessageHandler<AuthHeaderHandler>();

            builder.Services
                .AddHttpClient("basedataApi", c =>
                {
                    c.BaseAddress = new Uri("http://localhost:5800");
                    // c.BaseAddress = new Uri("http://dataserver.webapi");
                })
                .AddHttpMessageHandler<AuthHeaderHandler>();

            //services.AddHttpClientInterceptor();

            builder.Services.AddAdminBlazor();
            builder.Services.AddBaseDataBlazor();
            builder.Services.AddDataServerBlazor();
            builder.Services.AddProductionBlazor();

            return builder.Build();
        }
    }
}